<?php
/**
 * cms后台留言管理页
 * author wang
 */

define("PHP_IN",true);
require "../include/init.php";
require "checklogin.php";
require ROOT_PATH."cms/smart_connection.php";

$page = isset($_GET['page']) ? $_GET['page'] : 1 ;
$pagesize = 4;
$pagestart = ($page-1) * $pagesize ;

$sql = "SELECT count(*) AS num FROM guestbook";
$result = $db->query($sql);
$count = 0 ;
if($result && $db->num_rows($result)){
   $row = $db->fetch_one($result);
        $count = $row['num'];
}

$pagecount = ceil($count/$pagesize);
$page_links = cms_page_links($pagecount, $page, "guestbook_list.php?$page");

$sql = "SELECT * FROM guestbook ORDER BY msgid DESC LIMIT $pagestart,$pagesize ";
$result = $db->query($sql);
$guestbook = $db->fetch_all($result);

if(isset($_GET['act']) && $_GET['act']=="del"){
    $deleteid = isset($_GET['deleteid']) ? $_GET['deleteid'] : 0;
    $sql = "DELETE FROM guestbook WHERE msgid = $deleteid";
    $result = $db->query("$sql");
    if($result){
        $sql = "SELECT count(*) AS num FROM guestbook";
        $result = $db->query($sql);
        $row = $db->fetch_one($result);
        $count = $row['num'];

        $pagecount_now = ceil($count/$pagesize);
        if($page == $pagecount){
            if($pagecount_now < $pagecount){
                $page = $pagecount_now;
            }
        }

        header("Location:guestbook_list.php?page=".$page);
    }else{
        alert("删除失败");
    }
}

if(isset($_GET['act']) && $_GET['act']=="delall" && !empty($_POST['idarr'])){
    $deleteid = isset($_POST['idarr']) ? implode(",",$_POST['idarr']) : "";
    $sql = "DELETE FROM guestbook WHERE msgid IN ($deleteid)";
    $result = mysql_query("$sql");
    if($result){
        $sql = "SELECT count(*) AS num FROM guestbook";
        $result = mysql_query($sql);
        $row = mysql_fetch_assoc($result);
        $count = $row['num'];

        $pagecount_now = ceil($count/$pagesize);

        if($page == $pagecount){
            if($pagecount_now < $pagecount){
                $page = $pagecount_now;
            }
        }

        header("Location:guestbook_list.php?page=".$page);
    }else{
        alert("删除失败");
    }
}

$smarty->assign("guestbook",$guestbook);
$smarty->assign("page",$page);
$smarty->assign("pagecount",$pagecount);
$smarty->assign("pagesize",$pagesize);
$smarty->assign("page_links",$page_links);

$smarty->display("guestbook_list.tpl");
$db->close();
?>
